#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll f (ll x) {
    ll y = sqrt(x);
    while (y * y > x) y--;
    while ((y+1) * (y+1) <= x) y++;
    return y;
}

void solve() {
    ll C, D;
    cin >> C >> D;

    ll ans = 0;
    ll xmin = 1, xmax = 9, cshift = 10;
    while (xmin <= C + D) {
        ll l = max(xmin, C + 1);
        ll r = min(xmax, C + D);
        if (l <= r) {
            ll vl = (C * cshift) + l;
            ll vr = (C * cshift) + r;

            ans += (f(vr) - f(vl - 1));
        }
        xmin *= 10;
        xmax = (xmax + 1) * 10 - 1;
        cshift *=  10;
    }

    cout << ans << endl;

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t-- > 0) {
        solve();
    }

    return 0;
}